/**
 * 
 */
package br.com.io.beans;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import br.com.io.dao.ClienteDao;
import br.com.io.dao.DAO;
import br.com.io.modelo.Cliente;
import br.com.io.modelo.Ocorrencia;

/**
 * @author Lincoln
 *
 */
@ManagedBean(name ="ClienteBean")
@SessionScoped
@Named
public class ClienteBeans implements Serializable{
	
/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
private Session session;
	
		
	private List<Cliente> listCli;
	
	private List<Cliente> listCliNome;
	
	private  Long idcliente;
	
	
	public Long getIdcliente() {
		return idcliente;
	}


	public void setIdcliente(Long idcliente) {
		this.idcliente = idcliente;
	}


	public ClienteBeans(){
		
	}
	

public List<Cliente> getListCli() {
		return listCli;
	}


public List<Cliente> getListCliNome() {
	 Criteria c = getSession().createCriteria(Cliente.class);
     // c.add(Restrictions.ilike("ocorrencia", ocorrencia));
      return listCliNome = c.list();
	
}




public void setListCliNome(List<Cliente> listCliNome) {
	this.listCliNome = listCliNome;
}




public Session getSession() {
	return session;
}




public void setSession(Session session) {
	this.session = session;
}



public List<Cliente> buscaTodosClientes(){
	 Configuration configuration = new AnnotationConfiguration();
	    configuration.configure();
	    SessionFactory sf = configuration.buildSessionFactory();
	    Session s = sf.openSession();
    Criteria c = s.createCriteria(Cliente.class);
   // c.add(Restrictions.ilike("nomeCli", nome));
    System.out.println("Testando a busca");
    return  c.list();
}


public List<Cliente> buscaClientes(String nome){
    Criteria c = getSession().createCriteria(Cliente.class);
    c.add(Restrictions.ilike("nomeCli", nome));
    return c.list();
}


	
public void salva(Cliente cli){
	 System.out.println("Teste Adiciona!");
	 
	
	 Configuration configuration = new AnnotationConfiguration();
	   configuration.configure();
	     SessionFactory sf = configuration.buildSessionFactory();
	   Session s = sf.openSession();
	   System.out.println("Teste Adiciona!");
	   ClienteDao dao = new ClienteDao(s);
	  
	    s.beginTransaction();
		dao.salva(cli);
		s.getTransaction().commit();
		
		s.close();
	
	}
	
	public void remove(Cliente cli){
		
		this.session.delete(cli);
		
	}
	
	public Cliente procura(Long id){
		
		return(Cliente) this.session.load(Cliente.class, id);
	}
	
	public void atualiza(Cliente cli){
		this.session.update(cli);
	}
	
	public List<Cliente> listaTudo(){
		
		return this.session.createCriteria(Cliente.class).list();
		
	}
	
	public List<Cliente> pagina (int inicio, int quantia){
		
		return this.session.createCriteria(Cliente.class).setMaxResults(quantia).setFirstResult(inicio).list();
	}
	
	
	public List<Cliente> precoMaiorQue(double preco){
		
		Query query = this.session.createQuery("from Cliente where preco > :preco");
			
			query.setDouble("preco", preco);
			return query.list();
	}

	public String verificaUsuario(){
		System.out.println("test Lincoln");
		return null;
	}
	
}
